package com.mew.slots.core.game.klxg.room;

import com.mew.slots.core.common.game.BaseRoom;
import com.mew.slots.core.common.game.vo.BasePlayMsgs;
import com.mew.slots.core.game.klxg.bo.KlxgPlayMsgs;
import com.mew.slots.core.game.klxg.roller.Controlline;
import com.mew.slots.core.game.klxg.bo.KlxgInRoom;
import com.mew.slots.core.game.klxg.roller.KlxgIli;

import java.util.ArrayList;
import java.util.Arrays;

public class KlxgRoom extends BaseRoom {

    final int _FREE = 12;
    KlxgInRoom inRoom;
    boolean isChange;

    public KlxgRoom(KlxgInRoom inRoom,boolean isChange){
        this.inRoom=inRoom;
        this.isChange=isChange;
        res = new KlxgPlayMsgs();
        row = 3;
        col = 5;
        _WILD = 11;
        spes = new ArrayList<Integer>(){{
            add(_FREE);
            add(-1);
        }};
        rate = new int[][]{
                {0, 0, 8, 20, 50},      //9
                {0, 0, 8, 20, 50},      //10
                {0, 0, 8, 20, 50},      //J
                {0, 0, 8, 20, 50},      //Q
                {0, 0,10, 20, 50},      //K
                {0, 0,10, 20, 50},      //A
                {0, 0,10, 30, 80},      //项圈
                {0, 0,10, 30, 80},      //牛奶
                {0, 2,10, 30, 80},      //狗娘
                {0, 4,20, 50, 80},      //骨头
                {0, 4,20, 50,100},      //小女孩

                {0, 0, 0,  0,  0},      //WILD
                {0, 0, 0,  0,  0}       //FREE
        };
        alllines=new int[][]{
                {5,6,7,8,9},
                {0,1,2,3,4},
                {10,11,12,13,14},
                {0,6,12,8,4},
                {10,6,2,8,14},
                {5,1,7,13,9},
                {5,11,7,3,9},
                {0,1,7,13,14},
                {10,11,7,3,4},
                {0,6,2,8,4},
                {10,6,12,8,14},
                {5,1,2,3,9},
                {5,11,12,13,9},
                {0,6,7,8,4},
                {10,6,7,8,14},
                {5,6,2,8,9},
                {5,6,12,8,9},
                {5,1,7,3,9},
                {5,11,7,13,9},
                {0,1,2,8,14},
                {10,11,12,8,4},
                {0,6,12,13,14},
                {10,6,2,3,4},
                {5,6,7,3,14},
                {5,6,7,13,4},
                {0,6,7,8,14},
                {10,6,7,8,4},
                {10,11,7,3,9},
                {0,1,7,13,9},
                {10,6,12,13,14},
                {0,6,2,3,4},
                {0,6,12,8,9},
                {10,6,2,8,9},
                {5,1,7,3,4},
                {5,11,7,13,14},
                {0,6,7,8,9},
                {10,6,7,8,9},
                {5,1,2,3,4},
                {5,11,12,13,14},
                {5,6,7,8,4},
                {5,6,7,8,14},
                {0,1,7,3,4},
                {10,11,7,13,14},
                {0,1,2,8,4},
                {10,11,12,8,14},
                {5,6,2,3,4},
                {5,6,12,13,14},
                {0,1,7,8,4},
                {10,11,7,8,14},
                {10,6,2,8,4}
        };
    }

    /**
     * 取线
     */
    @Override
    public void getLinesXml(){

        if (controlType>0){
            endSlots = control();
        } else {
            endSlots = rollerXml.getLineInts( free ? 1 : 0,rollerID);
        }
        res.endSlots = endSlots.clone();
        replaceEndSlots();
    }
    /**
     * 取线
     */
    @Override
    public void getLinesIli(){

        if (controlType>0){
            endSlots = control();
        } else {
            endSlots = getIliEndSlots();

        }
        res.endSlots = endSlots.clone();
        replaceEndSlots();
    }

    public void replaceEndSlots(){
        KlxgPlayMsgs pmres= (KlxgPlayMsgs) res;
        if(free){
            for (int i = 0; i < endSlots.length; i++) {
                if(endSlots[i]==_WILD&&inRoom.getLock()[i]<3){
                    inRoom.getLock()[i]++;
                    pmres.getLock()[i]=inRoom.getLock()[i];
                }
            }
            for (int i = 0; i < inRoom.getLock().length; i++) {
                if (inRoom.getLock()[i] > 0) {
                    endSlots[i] = _WILD;
                }
            }
            return;
        }
        if(isChange){
            boolean hasWild=false;
            //找新狗头
            for (int c = 1; c < col; c++) {
                for(int r = 0; r<row; r++){
                    if(endSlots[r*5+c]==_WILD&&inRoom.getLock()[r*5+c]==0){
                        inRoom.getLock()[r*5+c]=1;
                        pmres.getLock()[r*5+c]=1;
                        hasWild=true;
                    }
                }
            }
            //替换狗头
            for (int i = 0; i < inRoom.getLock().length; i++) {
                if (inRoom.getLock()[i] > 0) {
                    endSlots[i] = _WILD;
                }
            }
           // 锁定中  第一列-1  避免算分
            if(hasWild){
                for (int i = 0; i < row; i++) {
                    endSlots[col*i]=-1;
                }
            }else{//锁定结束
                for (int i = 0; i < inRoom.getLock().length; i++) {
                    inRoom.getLock()[i]=0;
                }
            }
            res.setChange(hasWild);
        }
    }

    @Override
    public void checkaward(){
        int[] rl;
        int type;
        int num;

        for (int j = 0;j< iconLines.length;j++) {

            BasePlayMsgs.LineResult bgo = new BasePlayMsgs.LineResult();
            bgo.lineNum = j;
            rl = iconLines[j].clone();
            type = rl[0];
            num = 0;
            //不为特殊图标
            if (spes.contains(type)){
                continue;
            }

            //确定类型
            for(int i=0;i<rl.length;i++){
                if(rl[i]!=_WILD){
                    type=rl[i];
                    break;
                }
            }
            int wildRate=1;
            for (int i = 0; i < rl.length; i++) {
                if (type==rl[i]) {
                    num++;
                }else if(rl[i] == _WILD){
                    if(free) {
                        wildRate *= inRoom.getLock()[lines[j][i]] > 0 ? inRoom.getLock()[lines[j][i]] : 1;
                    }
                    num++;
                }else {
                    break;
                }
            }
            Integer rate0 = rate[type][num - 1];
            if (rate0>0){
                bgo.mult = rate0*wildRate;
                bgo.type = type;
                bgo.num = num;
                bgo.uvIdx = Arrays.stream(lines[j]).limit(num).toArray();
                res.lines.add(bgo);
            }

        }

        for(BasePlayMsgs.LineResult x:res.lines)
        {
            res.multiple += x.mult;
        }
    }
    @Override
    public void freeBouns(){
        KlxgPlayMsgs pmres= (KlxgPlayMsgs) res;
        int freeNum = fsdb(_FREE);
        if(free) {
            if(freeNum>0){
                pmres.setFree((byte)1);
                pmres.setFreeNumber(freeNum);
            }
            return;
        }
        if (freeNum > 2) {
            pmres.setFree((byte) 1);
            pmres.setFreeNumber(10);
            pmres.setBounswin(res.getBet() * res.getLine() * 7);
            return;
        }
        if(!free&&!isChange&&llx(_WILD,1,2,3,4)){
            for (int i = 0; i < endSlots.length; i++) {
                if(endSlots[i]==_WILD){
                    inRoom.getLock()[i]=1;
                    pmres.getLock()[i]=1;
                }
            }
            pmres.setChange(true);
        }
    }
    @Override
    public int[] control(){
        KlxgIli instance = KlxgIli.getInstance();
        switch (controlType) {
            case 1:
                instance.getSpeints("21-23",in);
            case 2:
                return instance.getSpeints("sfree",in);
            case 3:
                return instance.getSpeints("bonus",in);
            default:
                return instance.getSpeints("0-0",in);
        }
    }

    /*@Override
    public int[] control(){
        Controlline controlline=new Controlline();
        switch (controlType) {
            case 1:
                return bigwin();
            case 2:
                return controlline.free();
            case 3:
                return controlline.wild();
            default:
                return controlline.empty();
        }
    }*/
}
